package de.jmda.fx.node;

import java.util.LinkedList;
import java.util.List;

import javafx.event.Event;
import de.jmda.fx.node.behaviour.Behaviour;

/**
 * {@link javafx.event.EventHandler} implementation that allows <b>multiple</b>
 * ways of handling a <b>single</b> event (see {@link #handle(Event)}).
 *
 * @param <T>
 */
public class EventHandler<T extends Event>
		implements javafx.event.EventHandler<T>
{
	/**
	 * list of ways to handle an event
	 */
	private List<Behaviour<T>> behaviours = new LinkedList<Behaviour<T>>();

	public EventHandler()
	{
		super();
	}

	/**
	 * Process the list of {@link #behaviours} and invoke {@link
	 * Behaviour#showBehaviour(Event)} for each item. This allows multiple ways
	 * of handling a single event.
	 *
	 * @see javafx.event.EventHandler#handle(javafx.event.Event)
	 */
	@Override
	public void handle(T event)
	{
		for (Behaviour<T> behaviour : behaviours)
		{
			behaviour.showBehaviour(event);
		}
	}

	public void add(Behaviour<T> behaviour)
	{
		behaviours.add(behaviour);
	}

	public void remove(Behaviour<T> behaviour)
	{
		behaviours.remove(behaviour);
	}
}